package com.lakers.notice.springboot.help;

import cn.hutool.json.JSONUtil;
import com.lakers.notice.springboot.db.MessageDo;
import com.lakers.notice.springboot.db.MessageUserDo;
import com.lakers.notice.springboot.db.StorageHelp;
import com.lakers.notice.springboot.model.NoticeMessageInfo;
import com.lakers.notice.springboot.model.NoticeMessageQueryInfo;
import com.lakers.notice.springboot.model.NoticePageResult;
import com.lakers.notice.springboot.model.NoticeReadStatus;
import com.lakers.notice.springboot.model.NoticeRecvInfo;
import com.lakers.notice.springboot.model.NoticeSendMessage;
import com.lakers.notice.springboot.model.NoticeUserInfo;
import com.lakers.notice.springboot.netty.CmdType;
import com.lakers.notice.springboot.netty.processor.ProcessorFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述: NoticeMessageHelp
 *
 * @author liqin(zxl)
 * @date 2024/5/3
 */
@Component
@Slf4j
public class NoticeMessageHelp {

    @Resource
    private StorageHelp storageHelp;

    public Long save(NoticeSendMessage message){
        MessageDo messageDo = new MessageDo();
        messageDo.setContent(message.getContent());
        messageDo.setRecvIds(String.join(",", message.getRecvIds()));
        messageDo.setSendId(message.getSender().getUserId());
        messageDo.setBusinessType(message.getBusinessType());
        messageDo.setBusinessId(message.getBusinessId());
        messageDo.setType(message.getType());
        return storageHelp.save(messageDo);
    }


    public void trySave(String userId, List<Long> messageId){
        final List<MessageUserDo> messageUsers = messageId.stream().map(e -> {
            MessageUserDo messageUser = new MessageUserDo();
            messageUser.setUserId(userId);
            messageUser.setNoticeId(e);
            return messageUser;
        }).collect(Collectors.toList());
        storageHelp.trySave(messageUsers);
    }

    public Integer unReadCount(String userId){
        NoticeMessageQueryInfo messageQuery = new NoticeMessageQueryInfo();
        messageQuery.setUserId(userId);
        messageQuery.setStatus(NoticeReadStatus.un_read.getCode());
       return storageHelp.messageCount(messageQuery);
    }

    public NoticePageResult<NoticeMessageInfo> getMessages(NoticeMessageQueryInfo messageQuery){
        final Integer count = storageHelp.messageCount(messageQuery);
        if (count <=0){
            return new NoticePageResult<>();
        }
        final List<MessageDo> messages = storageHelp.messageList(messageQuery);
        return new NoticePageResult<>(count,  messages.stream().map(ele -> {
            NoticeMessageInfo info = new NoticeMessageInfo();
            info.setBusinessId(ele.getBusinessId());
            info.setBusinessType(ele.getBusinessType());
            info.setSendId(ele.getSendId());
            info.setSendTime(ele.getSendTime());
            info.setContent(ele.getContent());
            info.setMessageId(String.valueOf(ele.getMessageId()));
            info.setType(ele.getType());
            info.setStatus(ele.getStatus());
            return info;
        }).collect(Collectors.toList()));

    }


    public void send(NoticeSendMessage message, String messageId){
        NoticeRecvInfo recvInfo = new NoticeRecvInfo();
        recvInfo.setCmd(CmdType.MESSAGE.getCmd());
        recvInfo.setData(message.getContent());
        recvInfo.setMessageId(messageId);
        List<NoticeUserInfo> receivers = new ArrayList<>();
        for (Integer terminal : message.getRecvTerminals()) {
            for (String recvId : message.getRecvIds()) {
                receivers.add(new NoticeUserInfo(recvId, terminal));
            }
        }
        recvInfo.setReceivers(receivers);
        recvInfo.setSender(message.getSender());
        ProcessorFactory.createProcessor(CmdType.MESSAGE).process(recvInfo);
    }

}
